var path = '..';
var imagePath = 'http://image.6yi.ren';
var gl_vue_vm_list_html = $('#vue_vm_list').html();
var view = null;
var elderFamilyMemberView = null;

$(function() {
	$('#vue_vm_list div').hide();
});

function login() {
	removeLoginedUser();
	window.location.href = 'login.html';
}

function logout() {
	if(confirm('确定要退出登录吗？')) {
		removeLoginedUser();
		window.location.href = 'login.html';
	}
}

function setLoginedUser(loginedUser) {
	var loginedUserStr = JSON.stringify(loginedUser);
	$.cookie('loginedUser', loginedUserStr);
}

function removeLoginedUser() {
	$.cookie('loginedUser', "unlogined");
}

function getLoginedUser() {
	var loginedUserStr = $.cookie('loginedUser');
	
	if(!loginedUserStr || loginedUserStr=='unlogined') {
		if(confirm('请您先登录，是否现在登录吗？')) {
			window.location.href = 'login.html';
		}
		
		return null;
	}
	
	var loginedUser = JSON.parse(loginedUserStr);
	return loginedUser;
}

function getLoginedUserForHome() {
	var loginedUserStr = $.cookie('loginedUser');
	
	if(!loginedUserStr || loginedUserStr=='unlogined') {
		return null;
	}
	
	var loginedUser = JSON.parse(loginedUserStr);
	return loginedUser;
}

function find_basic_by_json(url,dataModel,onSuccess) {
	openWaiting('正在处理数据');
	// var dataStr = JSON.stringify(dataModel);
	// console.log(dataStr);
	$.post(url, dataModel, function(response) {
		closeWaiting();

		if(response.code=='200') {
			//to do something
			if(onSuccess) {
				onSuccess(response);
			}else {
				alert('数据处理成功');
			}
		}else {
			alert(response.info);
		}

		console.log(response);
	});
}

function handle_basic(saveUrl, dataModel, onSuccess) {
	openWaiting('正在处理数据');
	var dataStr = JSON.stringify(dataModel);
	$.post(saveUrl, {data: dataStr}, function(response) {
		closeWaiting();
		
		if(response.code=='200') {
			//to do something
			if(onSuccess) {
				onSuccess(response);
			}else {
				alert('数据处理成功');
			}
		}else {
			alert(response.info);
		}
		
		console.log(response);
	});
};

function save_basic(saveUrl, dataModel, onSuccess) {
	openWaiting('正在保存数据');
	var dataStr = JSON.stringify(dataModel);
	$.post(saveUrl, {data: dataStr}, function(response) {
		closeWaiting();
		
		if(response.code=='200') {
			if(onSuccess) {
				onSuccess(response);
			}else {
				alert('数据保存成功');
			}
		}else {
			alert(response.info);
		}
		
		console.log(response);
	});
};

function findAll_basic(url, onSuccess) {
	openWaiting('正在查询');
	$.post(url, {}, function(response) {
		closeWaiting();
		
		if(response.code=='200') {
			if(response.data.length==0) {
				alert('找不到记录');
				return;
			}
			
			if(onSuccess) {
				onSuccess(response);
			}
		}else {
			alert(response.info);
		}
		
		console.log(response);
	});
}

function find_basic(url, searchVal, onSuccess) {
	if(!searchVal || searchVal=='') {
		alert('请输入查找条件');
		return;
	}
	
	openWaiting('正在查询');
	
	$.post(url, {searchVal:searchVal}, function(response) {
		closeWaiting();
		
		if(response.code=='200') {
			if(response.data.length==0) {
				alert('找不到符合条件的记录');
				return;
			}
			
			if(onSuccess) {
				onSuccess(response);
			}
		}else {
			alert(response.info);
		}
		
		console.log(response);
	});
}

function save_basic_by_json(saveUrl, json, onSuccess) {
	openWaiting('正在保存数据');
	$.post(saveUrl, json, function(response) {
		closeWaiting();
		
		if(response.code=='200') {
			//to do something
			if(onSuccess) {
				onSuccess(response);
			}else {
				alert('数据保存成功');
			}
		}else {
			alert(response.info);
		}
		
		console.log(response);
	});
};

function find_basic_by_json(url, json, onSuccess) {
	if(!json) {
		alert('请输入查找条件');
		return;
	}
	
	openWaiting('正在查询');
	
	$.post(url, json, function(response) {
		closeWaiting();
		
		if(response.code=='200') {
			if(response.data.length==0) {
				alert('找不到符合条件的记录');
				return;
			}
			
			if(onSuccess) {
				onSuccess(response);
			}
		}else {
			alert(response.info);
		}
		
		console.log(response);
	});
}

function deleteBatch_basic_by_array(url, itemIdArray, onSuccess) {
	if(itemIdArray.length == 0) {
		alert('请先指定所要删除的项');
		return;
	}
	
	if(confirm('确实要删除所选项吗？')) {
		openWaiting('正在删除');
		
		var dataStr = JSON.stringify({ids:itemIdArray});
		
		$.post(url, {data: dataStr}, function(response) {
			closeWaiting();
			
			if(response.code=='200') {
				if(onSuccess) {
					onSuccess();
				}else {
					alert('删除成功');
				}
			}else {
				alert(response.info);
			}
			
			console.log(response);
		});
	}
};

function deleteBatch_basic(url) {
	var itemIdStr = '';
	var itemIdArray = [];
	
	$('ul').each(function() {
		if($(this).find('.magic-checkbox').is(':checked')) {
			var itemId = $(this).find('.magic-checkbox').attr('id');
			itemIdStr += (itemIdStr==''? itemId: ','+itemId);
			itemIdArray.push(itemId);
		}
	});
	
	if(itemIdArray.length == 0) {
		alert('请先勾选所要删除的项');
		return;
	}
	
	if(confirm('确实要删除所选项吗？')) {
		openWaiting('正在删除');
		
		var dataStr = JSON.stringify({ids:itemIdArray});
		
		$.post(url, {data: dataStr}, function(response) {
			closeWaiting();
			
			if(response.code=='200') {
				$('ul').each(function() {
					if($(this).find('.magic-checkbox').is(':checked')) {
						$(this).remove();
					}
				});
				
				alert('删除成功');
			}else {
				alert(response.info);
			}
			
			console.log(response);
		});
	}
};

function deleteOne_basic(url, id) {
	openWaiting('正在删除');
	$.post(url, {id: id}, function(response) {
		closeWaiting();
		
		if(response.code=='200') {
			alert('删除成功');
		}else {
			alert(response.info);
		}
		
		console.log(response);
	});
};

function _addChangeListener(obj, fun) {
	$(obj).change(function() {
		try {
			if(fun) {
				fun();
			}
		} catch(e) {
			alert('加载数据出现异常：' + e);
		}
	});
}

function find(url) {
	if($('#vue_vm_list').length==0) {return;}
	$('#vue_vm_list div').hide();
	var searchVal = $('#searchVal').val();
	
	openWaiting('正在查找');
	
	find_basic(path+'/' + url, searchVal, function(response) {
		closeWaiting();
		
		$('#vue_vm_list').html(gl_vue_vm_list_html);
		
		new Vue({
			el: '#vue_vm_list',
			data: {
				todos: response.data
			}
		});
		
		$('#vue_vm_list div').show();
	});
}


//将url '?'后面的参数转为对象形式
function getSearchObj() {
	var qs = location.search.length > 0 ? location.search.substr(1) : '',
		args = {},
		items = qs.length > 0 ? qs.split('&') : [],
		item = null, name = null, value = null, i = 0, len = items.length;

	for (i = 0; i < len; i++) {
		item = items[i].split('=');
		name = decodeURIComponent(item[0]);
		value = decodeURIComponent(item[1]);

		if (name.length) {
			args[name] = value;
		}
	}
	return args;
}

function doOpenWaiting(val){
    openWaiting(val);
}